<?php

namespace app\home\controller;


use think\facade\App;
use think\facade\Request;

class UrlChannelRelation extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 我的渠道
     */
    public function index() {
        $params = [
            'startDate'   => input('startDate', '', 'trim'),
            'endDate'     => input('endDate', '', 'trim'),
            'keyword'     => input('keyword', '', 'trim'),
            'link'        => input('link', '', 'trim'),
            'channelName' => input('channelName', '', 'trim'),
            'channelKey'  => input('channelKey', '', 'trim'),
            'channelCode' => input('channelCode', '', 'trim'),
            'pageSize'    => input('pageSize', 10)
        ];

        $data = model('UrlChannelRelation', 'logic')->index($params, $this->userId);
        $this->assign($data);
        return view();
    }

    /**
     * @desc 我的渠道
     */
    public function detail() {
        $params['id']    = input('id', 0);
        $params['urlId'] = input('urlId', 0);

        $data = model('UrlChannelRelation', 'logic')->detail($params, $this->userId);
        $this->assign($data);
        return view();
    }

    /**
     * @desc 添加分组
     */
    public function add() {
        $params = [
            'urlId'     => input('urlId/a'),
            'channelId' => input('channelId')
        ];
        $logic = model('UrlChannelRelation', 'logic');
        $data = [];
        try {
            $data = $logic->add($params, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
    
    /**
     * @desc 创建链接
     */
    public function del() {
        $id = input('id');
        
        $logic = model('UrlChannelRelation', 'logic');
        $data = [];
        try {
            $data = $logic->del($id, $this->userId);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
}
